/*
 * Copyright (C) 2009 Umin (umin90@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include "player.h"

int fileType;
AudioPlayer* player;
extern struct info;

createAudioPlayer(char* name){
    strlwr(name);
    if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".mp3")
        player = AudioPlayer::create_audio_player(FS_MP3_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".aa3")
        player = AudioPlayer::create_audio_player(FS_AA3_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".oma")
        player = AudioPlayer::create_audio_player(FS_AA3_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".omg")
        player = AudioPlayer::create_audio_player(FS_AA3_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".aac")
        player = AudioPlayer::create_audio_player(FS_AAC_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".m4a")
        player = AudioPlayer::create_audio_player(FS_M4A_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".mp4")
        player = AudioPlayer::create_audio_player(FS_M4A_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".at3")
        player = AudioPlayer::create_audio_player(FS_AT3_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".wav")
        player = AudioPlayer::create_audio_player(FS_WAV_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".ogg")
        player = AudioPlayer::create_audio_player(FS_OGG_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".ape")
        player = AudioPlayer::create_audio_player(FS_MAC_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".tta")
        player = AudioPlayer::create_audio_player(FS_TTA_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".mpc")
        player = AudioPlayer::create_audio_player(FS_MPC_FILE);
    else if(!strcmp(strsub(name,strlen(name)-4,strlen(name)),".wma")
        player = AudioPlayer::create_audio_player(FS_WMA_FILE);
    else if(!strcmp(strsub(name,strlen(name)-3,strlen(name)),".wv")
        player = AudioPlayer::create_audio_player(FS_WAVPACK_FILE);
    else if(!strcmp(strsub(name,strlen(name)-5,strlen(name)),".flac")
        player = AudioPlayer::create_audio_player(FS_FLAC_FILE);
    else error(strcat("Unkonw File Type:",name),0);
}

void Music_Init(){
    char *code;
    code = AudioPlayer::init_hw_codec();
    if(code)nge_error("Music init fault:",code);
}


void Music_Load(const char * filename){
    info->currentTime = 0;
    info->lengthTime = 0;
    
    player = createAudioPlayer(filename);
    player->open(filename, 0, -1);
}
char* Music_Play(){
    return player->play();
}
void Music_Pause(){
    player->pause();
}
void Music_Stop(){
    player->stop();
}
void Music_Resume(){
    player->resume();
}
int32_t Music_GetPos(){
    return player->get_current();
}
void Music_SetPos(int32_t pos){
    player->seek(pos);
}
int32_t  Music_GetTotalTime(){  //Sec
    if(player!=0)
        return (info->lengthTime / 1000000) % 60;
}
int32_t  Music_GetCurrentTime(){ //MSec
    return (info->currentTime /1000) % 60;
}
int32_t  Music_Samplerate(){
    return info->samplerate;
}
int32_t  Music_Channel(){
    return info->channels;
}
int Music_Eos(){
    return player->eof();
}
void Music_End(){
    Music_Stop();
    info->currentTime = 0;
    info->lengthTime = 0;
    delete player;
}